/**
 * 用户注册
 */
$(function (){
	init_reg_user();
	
	init_login_user();
	
	init_find_user();
});

function init_reg_user()
{
	if (typeof(currentUid) != 'undefined')
		$("#btn_reg").click(dochguser);
	else{
		//$('#email').blur(mailcheck);
		$("#btn_reg").click(doreguser);
	}
	
	var currentYear = new Date().getFullYear();
	var yearRange = (currentYear - 100) + ":" + currentYear;
	
	if ($("#birth").size() > 0)
	$("#birth").datepicker({buttonImageOnly: true,buttonImage: "images/calendar.png",showOn: "button",
		changeMonth:true, changeYear: true,yearRange: yearRange,constrainInput:true,constrainInput:true});	
}

function init_login_user()
{
	$("#btn_login").click(dologin);
}

function init_find_user()
{
	$("#btn_find").click(dofind);
}

function mailcheck()
{
	if (typeof(plan) == 'undefined'){
		plan = "";
	}
	var url = baseUrl + "/?a=mailcheck&plan="+plan;
	$.post(url, {email:$('#email').val()}, function (ret){
		if (ret.status == 0)
			alert(ret.message);
	}, "json");
}

function doreguser()
{
	if (typeof(plan) == 'undefined'){
		plan = "";
	}
	var fields = ['email', 'password', 'password2', 'enfname', 'enlname', 'name', 'gender',
	'nation', 'birth', 'idcode', 'unit', 'cnunit', 'headphoto', 'idphoto', 'phone', 'fax', 'address', 'pcode','gourl' ];
	
	var mast_fields = ['email', 'password', 'password2', 'enfname', 'enlname', 'name', 'gender',
	'nation', 'idcode', 'unit',  'headphoto','phone', 'address', 'pcode'];
	
	var mast_fields_msg = ['邮件地址', '用户密码', '确认密码', '英文姓', '英文名', '中文姓名', '性别',
	'国家或地区',  '身份证号码', '工作单位（中文名称）', '职务（中文名称）','联系电话',  '联系地址', '邮政编码'];
	var tipmessage = " 不能为空！";
	var sucessmessage = "统一身份认证注册成功！\n请继续填写会议注册信息...";
	
	if (typeof(plan) != 'undefined' && plan == 'en'){
		mast_fields = ['email', 'password', 'password2', 'enfname', 'enlname', 'name', 'gender',
		           	'nation', 'idcode', 'cnunit', 'idphoto', 'phone','address', 'pcode'];
		
		mast_fields_msg = ['E-mail', 'Password', 'Confirm Password', 'Family Name', 'Given Name', 'Full Name', 'Gender',
		               	'Country/Region', 'Passport No.', 'Affiliation','Position', 'Telephone', 'Mailing Address', 'Zip Code'];
		tipmessage = " is required.";
		sucessmessage = "Registration of Unified Identity of Conference is successful!\nPlease continue to fill in Conference Registration Information...";
	}
	
	var param = {};
	
	// 数据载入
	for(var i = 0; i<fields.length; i++){
		param[fields[i]] = $('#' + fields[i]).val();
	}
	
	// 必填判断
	for(var i = 0; i<mast_fields.length; i++){
		var val = $('#' + mast_fields[i]).val();
		if ($.trim(val).length <= 0){
			alert(mast_fields_msg[i] + tipmessage);
			return;
		}
	}

	var url = baseUrl + "/?a=doreg&plan="+plan;
	
	$.post(url, param, function (ret){
		if (ret.status == 0)
			alert(ret.message);
		else{
			alert(sucessmessage);
			location.href=ret.gourl;
		}
	}, "json");
}


// 修改用户信息
function dochguser()
{
	if (typeof(plan) == 'undefined'){
		plan = "";
	}
	
	var fields = ['enfname', 'enlname', 'name', 'gender',
	          	'nation', 'birth', 'idcode', 'unit', 'phone', 'fax', 'address', 'pcode','gourl', "idphoto", "headphoto","cnunit"];
	          	
	var mast_fields = ['enfname', 'enlname', 'name', 'gender',
	               	'nation', 'idcode', 'unit',  "headphoto",'phone', "address", "pcode"];
	               	
	               	var mast_fields_msg = ['英文姓', '英文名', '中文姓名', '性别',
	               	'国家或地区',  '身份证号码', '工作单位（中文名称）', '职务（中文名称）','联系电话',  "联系地址", "邮政编码"];
	
	var tipmessage = " 不能为空！";
	var sucessmessage = "统一身份认证修改成功！";
	
	if (typeof(plan) != 'undefined' && plan == 'en'){
		mast_fields = [ 'enfname', 'enlname', 'name', 'gender',
			           	'nation', 'idcode', 'cnunit', 'idphoto', 'phone',"address", "pcode"];
			
			mast_fields_msg = [ 'Family Name', 'Given Name', 'Full Name', 'Gender',
			               	'Country/Region', 'Passport No.', 'Affiliation','Position', 'Telephone', 'Mailing Address', 'Zip Code'];
		tipmessage = " is required.";
		sucessmessage = "Modification of Unified Identity of Conference is successful!";
	}
  	var param = {};
  	
  	// 数据载入
  	for(var i = 0; i<fields.length; i++){
  		param[fields[i]] = $('#' + fields[i]).val();
  	}
  	
  	// 必填判断
  	for(var i = 0; i<mast_fields.length; i++){
  		var val = $('#' + mast_fields[i]).val();
  		if ($.trim(val).length <= 0){
  			alert(mast_fields_msg[i] + tipmessage);
  			return;
  		}
  	}
  	
  	var url = baseUrl + "/?a=dochange&plan="+plan;
  	

	if (typeof(currentMid) != 'undefined'){
		url += '&mid=' +currentMid;
	}
	
	
  	$.post(url, param, function (ret){
  		if (ret.status == 0)
  			alert(ret.message);
  		else{
  			alert(sucessmessage);
  			if (typeof(currentMid) != 'undefined'){
  				location.href=ret.gourl;
  			}
  		}
  	}, "json");
}


function setPhoto(result, type)
{
	if (type == 'head'){
		$("#image_headphoto").attr("src",baseUrl + result.tbimage).click(function (){
			window.open(baseUrl+result.image);
		});
		$("#image_headphoto").parent().parent().show();
		$("#headphoto").val(result.image);
	}else if (type == 'idcode'){
		$("#image_idphoto").attr("src",baseUrl + result.tbimage).click(function (){
			window.open(baseUrl+result.image);
		});
		$("#image_idphoto").parent().parent().show();
		$("#idphoto").val(result.image);
	}
}


function dologin(){
	if (typeof(plan) == 'undefined'){
		plan = "";
	}
	
	var param = {};
	param.email = $('#login_email').val();
	param.password = $('#login_password').val();
	param.gourl = $('#gourl').val();
	param.authcode = $('#authcode').val();
	
	var url = baseUrl + "/?a=login&plan="+plan;;
	$.post(url, param, function (ret){
		if (ret.status == 0){
			if (ret['type'] != null && ret.type == 1000){
				getCode();
			}
			alert(ret.message);
		}else{
			location.href=ret.gourl;
		}
	}, "json");	
}

var g_find_lock = false;
function dofind()
{	
	if (typeof(plan) == 'undefined'){
		plan = "";
	}
	if (g_find_lock){
		return;
	}
	
	g_find_lock = true;
	var email = $('#login_email').val();
	var m_name = $('#m_name').val();
	var url = baseUrl + "/?a=find&plan="+plan;

	if (typeof(currentMid) != 'undefined'){
		url += '&mid=' +currentMid;
	}
	
	$.post(url, {email:email, m_name:m_name}, function (ret){
		g_find_lock = false;
		if (ret.status == 0)
			alert(ret.message);
		else{
			if (typeof(plan) != 'undefined' && plan == 'en'){
				alert("The password has been sent to your mailbox, please check it.");
			}else{
				alert("密码已经发送到您的邮箱，请查收。");
			}
			
		}
	}, "json");	
}


function doChangePwd()
{
	var oldpassword = $("#oldpassword").val();
	var password = $("#password").val();
	var password2 = $("#password2").val();
	
	var tip1 = "请填写原始密码";
	var tip2 = "请填写新密码";
	var tip3 = "两次密码不一致";
	var tip4 = "密码修改成功";

	var url = baseUrl + "/?a=dochangepwd&plan=";
	if (typeof(plan) != 'undefined'){
		if (plan == 'en'){
			tip1 = "Please input original password.";
			tip2 = "Please input new password.";
			tip3 = "Two passwords can't match.";
			tip4 = "Password modification is successful.";
			url += "en";
		}
	}
	
	if (oldpassword == ''){
		alert(tip1);
		return false;
	}
	
	if (password == ''){
		alert(tip2);
		return false;
	}
	
	if (password != password2){
		alert(tip3);
		return false;
	}
	
	$.post(url, {password:password, oldpassword:oldpassword}, function (ret){
		g_find_lock = false;
		if (ret.status == 0)
			alert(ret.message);
		else{
			alert(tip4);
			if (typeof(currentUrl) != 'undefined'){
				location.href=currentUrl;
			}else
				location.href=baseUrl+"/";
		}
	}, "json");	
}